/*
 * @Author: xiaosihan 
 * @Date: 2024-07-01 23:22:54 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-07-02 02:41:45
 */

import { DoubleSide, Group, MeshStandardMaterial } from "three";
import MeshBase from "three-base/MeshBase";
import carBodyGLB from "./车垫.glb?url";
import carShadowGLB from "./影子.glb?url";
import { carBodyMaterial } from "../materials/carBodyMaterial";
import { interiorMaterial } from "../materials/interiorMaterial";
import threeUtils from "three-base/threeUtils";
import { carMatsMaterial } from "../materials/carMatsMaterial";

// 加载汽车模型
export default class Car extends Group {
    constructor() {
        super();
    }

    //汽车模型
    carMesh = (() => {
        const carMesh = new MeshBase(carBodyGLB);
        carMesh.addEventListener("loaded", (e: any) => {

            threeUtils.alignBottom(carMesh, 0);

            carMesh.traverseMesh(mesh => {

                mesh.geometry.computeVertexNormals();

                if (["Obj3d66-14777498-35-810002"].includes(mesh.name)) {
                    mesh.material = carBodyMaterial;
                } else if (["花型-后排主驾驶"].includes(mesh.name)) {
                    mesh.visible = false;
                } else if (
                    [
                        "Obj3d66-14777498-23-166004",
                        "上层垫004", "Obj3d66-14777498-28-516004",
                        "上层垫002",
                    ].includes(mesh.name)
                ) {
                    mesh.material = carMatsMaterial;

                } else {
                    mesh.material = interiorMaterial;
                }

                //three事件
                // mesh.userData = {
                //     cursor: "pointer",
                //     enableEvent: true,
                // };
                // mesh.addEventListener("click", e => {
                //     console.log(mesh.name);
                // });
            })

            this.add(carMesh);
        });

        return carMesh;
    })();

    //汽车影子
    carShadow = (() => {
        const carShadow = new MeshBase(carShadowGLB);
        carShadow.addEventListener("loaded", (e: any) => {

            carShadow.rotation.set(0, Math.PI / 2, 0);
            threeUtils.scaleTo(carShadow, 5.5);
            threeUtils.center(carShadow);

            this.add(carShadow);
            this.dispatchEvent(e);
        });
        return carShadow;
    })();

}